# InputManager

**Description:**

&emsp;&emsp;`InputManager` is a class for managing input contexts and actions. Input events can be listened for and handled by creating input contexts and actions, and then adding them to the input manager. Specific combinations of input contexts can be activated and deactivated by calling the `pushContext` and `popContext` methods. When an event is triggered, input events can be handled by registering global input event listeners.

**Class Object:** [InputManager Class](/docs/api/Class%20Object/InputManager).

**Usage:**
```tl
local InputManager = require("InputManager")
local inputManager = InputManager.CreateManager({
	context1 = {
		action1 = Trigger.KeyDown("W"),
	},
})
-- activate context1
inputManager:pushContext("context1")
-- add prefix "Input." to the listened action name
node:gslot("Input.action1", function()
	print("action1 triggered")
end)
-- remove context1 from the context stack
inputManager:popContext()
inputManager:destroy()
```

## getNode

**Type:** Function.

**Description:**

&emsp;&emsp;Gets the current input node. The input node is used to receive input events. It will be added to `Director.entry` automatically.

**Signature:**
```tl
getNode: function(self: InputManager): Node.Type
```

**Returns:**

| Return Type | Description |
| --- | --- |
| Node | The input node. |

## pushContext

**Type:** Function.

**Description:**

&emsp;&emsp;Adds an input context to the context stack. Temporarily disables the previous context, then activates the actions in the new context.

**Signature:**
```tl
pushContext: function(self: InputManager, contextNames: string | {string}): boolean
```

**Parameters:**

| Parameter | Type | Description |
| --- | --- | --- |
| contextNames | string | \{string} | The name of the context or a list of context names. |

**Returns:**

| Return Type | Description |
| --- | --- |
| boolean | Whether the context is successfully pushed. |

## popContext

**Type:** Function.

**Description:**

&emsp;&emsp;Removes the current input context from the context stack. Activates the previous context.

**Signature:**
```tl
popContext: function(self: InputManager, count?: number): boolean
```

**Parameters:**

| Parameter | Type | Description |
| --- | --- | --- |
| count | number | [optional] The number of contexts to be popped. Default is 1. |

**Returns:**

| Return Type | Description |
| --- | --- |
| boolean | Whether the context is successfully popped. |

## emitKeyDown

**Type:** Function.

**Description:**

&emsp;&emsp;Emits a key down event for input simulation.

**Signature:**
```tl
emitKeyDown: function(self: InputManager, keyName: KeyName)
```

**Parameters:**

| Parameter | Type | Description |
| --- | --- | --- |
| keyName | KeyName | The name of the key. |

## emitKeyUp

**Type:** Function.

**Description:**

&emsp;&emsp;Emits a key up event for input simulation.

**Signature:**
```tl
emitKeyUp: function(self: InputManager, keyName: KeyName)
```

**Parameters:**

| Parameter | Type | Description |
| --- | --- | --- |
| keyName | KeyName | The name of the key. |

## emitButtonDown

**Type:** Function.

**Description:**

&emsp;&emsp;Emits a button down event for input simulation.

**Signature:**
```tl
emitButtonDown: function(self: InputManager, buttonName: ButtonName, controllerId?: number)
```

**Parameters:**

| Parameter | Type | Description |
| --- | --- | --- |
| buttonName | ButtonName | The name of the button. |
| controllerId | number | [optional] The ID of the gamepad controller. Default is 0. |

## emitButtonUp

**Type:** Function.

**Description:**

&emsp;&emsp;Emits a button up event for input simulation.

**Signature:**
```tl
emitButtonUp: function(self: InputManager, buttonName: ButtonName, controllerId?: number)
```

**Parameters:**

| Parameter | Type | Description |
| --- | --- | --- |
| buttonName | ButtonName | The name of the button. |
| controllerId | number | [optional] The ID of the gamepad controller. Default is 0. |

## emitAxis

**Type:** Function.

**Description:**

&emsp;&emsp;Emits an axis event for input simulation.

**Signature:**
```tl
emitAxis: function(self: InputManager, axisName: AxisName, value: number, controllerId?: number)
```

**Parameters:**

| Parameter | Type | Description |
| --- | --- | --- |
| axisName | AxisName | The name of the axis. |
| value | number | The value of the axis, ranging from -1 to 1. |
| controllerId | number | [optional] The ID of the gamepad controller. Default is 0. |

## destroy

**Type:** Function.

**Description:**

&emsp;&emsp;Destroys the input manager and clears input event listeners.

**Signature:**
```tl
destroy: function(self: InputManager)
```